#include "../include/setting_url.hpp"


std::shared_ptr<Response> N404(HTTP* args){
    // std::shared_ptr<RequestHead> head=*(std::shared_ptr<RequestHead>*) args;
    std::shared_ptr<Response> resp(new Response(args->get_ReqHead()));
    std::string buf="404";
    resp->setStatus(404);
    resp->setDescribe("ERR");
    resp->setText(buf);
    return resp;
}

std::shared_ptr<Response> index(HTTP* args){
    std::shared_ptr<Response> resp(new Response(args->get_ReqHead()));
    struct stat st;
    std::shared_ptr<char> buf;
    if(stat("./index.html",&st)==0){
        std::shared_ptr<FILE> file(fopen("./index.html","r"));
        if(file.get()!=nullptr){
            int file_len;
            buf=std::shared_ptr<char>(new char[st.st_size]);
            while((file_len=fread(buf.get(),sizeof(char),st.st_size,file.get()))!=st.st_size){}
            std::string s(buf.get());
            resp->setText(s);
            resp->setStatus(200);
            resp->setDescribe("OK");
        }
    }else{
        std::string buf="NOT FIND!";
        resp->setStatus(404);
        resp->setDescribe("not find");
    }
    return resp;
}

std::shared_ptr<Response> download_myDoc(HTTP* args){
    std::shared_ptr<Response> resp(new Response(args->get_ReqHead()));
    struct stat st;
    if(stat("medias/work.pdf",&st)==0){
        std::shared_ptr<FILE> file(fopen("medias/work.pdf","r"));
        if(file.get()!=nullptr){
            int file_len;
            resp->setStatus(200);
            resp->setDescribe("OK");
            resp->setMsg("Content-Length",std::to_string(st.st_size));
            resp->setMsg("Content-type","application/octet-stream");
            resp->setMsg("Content-Disposition","attachment;filename=张金炜-本科-15581115514.pdf");
            std::string value=resp->returnResponse();
            if(write(args->client_fd,value.c_str(),value.size())!=resp->get_size()){
                resp->setClose(1);
                resp->setSend(1);
                return resp;
            }else{
                char buf[4096];
                int read_t,write_t;
                int sum=st.st_size;
                while((read_t=fread(buf,sizeof(char),std::min(sum,4096),file.get()))>0){
                    write_t=write(args->client_fd,buf,read_t);
                    if(write_t==0 || write_t==-1){
                        resp->setClose(1);
                        resp->setSend(1);
                        return resp;
                    }
                }
                resp->setClose(1);
                resp->setSend(1);
                return resp;
               
            }
        }
    }else{
        std::string buf="NOT FIND!";
        resp->setStatus(404);
        resp->setDescribe("not find");
    }
    return resp;
}


// while((file_len=fread(buf.get(),sizeof(char),st.st_size,file.get()))!=st.st_size){}
//             resp->setStatus(200);
//             resp->setDescribe("OK");
//             std::string value=resp->returnResponse();
//             if(write(args->client_fd,value.c_str(),value.size())!=resp->get_size()){
//                 resp->setClose(1);
//                 resp->setSend(1);
//                 return resp;
//             }else{
//                 if(write(args->client_fd,buf.get(),st.st_size)!=st.st_size){

//                     resp->setClose(1);
//                     resp->setSend(1);
//                     return resp;
//                 }else{
//                     resp->setSend(1);
//                     return resp;
//                 }
//             }


// _Media Media;
void configuration(){
    configurationMediaType();   // 媒体文件类型配置
    configurationStaticType();  // 静态文件类型配置
    Media.url("/media/(.*?)","medias/");
    urls.url("/media/(.*?)",ResponseMedia);

    Static.url("/static/(.*?)","static/");
    urls.url("/static/(.*?)",ResponseStaticFile);
    urls.url("",N404);
    urls.url("/",index);
    urls.url("/download/",download_myDoc);
    

    printf("url 配置完毕\n");
}